package com.tjsinfo.tjsoa.documentationflow.dangweifawen.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.WritableFont;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;

public class DengJiBuAction extends DispatchAction{
	private ISystemSpringDao springDao;
	public void setSpringDao(ISystemSpringDao springDao) {
		this.springDao = springDao;
	}

	public ActionForward dengjibuSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		jxl.write.WritableWorkbook wwb = null;
		try {
			
			String beginNO=request.getParameter("beginNO");
			String endNO=request.getParameter("endNO");
			String whereSQL=" fawendatetime like '%"+request.getParameter("yearNO")+"%' and state=1 and flowid <> 0 and ";
			if (beginNO!=null && !"".equals(beginNO)) {
				whereSQL+="wenhaonumber>="+beginNO+" and ";
			}
			if (endNO !=null && !"".equals(endNO)) {
				whereSQL+="wenhaonumber<="+endNO+" and ";
			}

			JSONArray array = new JSONArray();
			JSONObject object = new JSONObject();
			List<Map<String, String>> tempList = springDao.commonSelect(new String[]{"wenhaotype","fawendatetime","zhusong","biaoti","nigaorenname","fenshu","wenhaonumber"}, "SELECT wenhaotype,fawendatetime,zhusong,biaoti,nigaorenname,fenshu,wenhaonumber FROM t_dangweifawen_neirong where "+whereSQL+" fawendatetime is not null order by wenhaonumber");
			for (Map<String, String> map : tempList) {
				JSONObject json = new JSONObject();
				json.put("wenhaotype", map.get("wenhaotype"));
				json.put("fawendatetime", map.get("fawendatetime"));
				json.put("zhusong", map.get("zhusong"));
				json.put("biaoti", map.get("biaoti"));
				json.put("nigaorenname", map.get("nigaorenname"));
				json.put("fenshu", map.get("fenshu"));
				array.put(json);
			}

			object.put("root", array);
			response.getWriter().print(object);
			
			String uuid=ToolKit.getUUID();
			File f = new File("C:\\tempFile\\"+uuid+".xls");
			wwb = Workbook.createWorkbook(f);
			jxl.write.WritableSheet ws = wwb.createSheet("查询结果", 0);
			ws.addCell(new jxl.write.Label(0,0, "文号"));
			ws.addCell(new jxl.write.Label(1,0, "发文时间"));
			ws.addCell(new jxl.write.Label(2,0, "主送"));
			ws.addCell(new jxl.write.Label(3,0, "摘要"));
			ws.addCell(new jxl.write.Label(4,0, "拟稿人"));
			ws.addCell(new jxl.write.Label(5,0, "份数"));
			ws.addCell(new jxl.write.Label(6,0, "备注"));
			for (int i=0,j=1;i<tempList.size();i++,j++) {
				ws.addCell(new jxl.write.Label(0,j, tempList.get(i).get("wenhaotype")));
				ws.addCell(new jxl.write.Label(1,j, tempList.get(i).get("fawendatetime").substring(0,10)));
				ws.addCell(new jxl.write.Label(2,j, tempList.get(i).get("zhusong")));
				ws.addCell(new jxl.write.Label(3,j, tempList.get(i).get("biaoti")));
				ws.addCell(new jxl.write.Label(4,j, tempList.get(i).get("nigaorenname")));
				ws.addCell(new jxl.write.Label(5,j, tempList.get(i).get("fenshu")));
				ws.addCell(new jxl.write.Label(6,j, ""));
			}
			

			jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);
			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
			wcfFC.setBackground(jxl.format.Colour.RED);
			wwb.write();
			wwb.close();
			
			request.setAttribute("uuid", uuid);
			return null;
		} catch (Exception e) {
			wwb.write();
			wwb.close();
			e.printStackTrace();
			return null;
		}
	}
	
}
